<template>
  <div class="app-container">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
              <el-form-item label="演员姓名" prop="name">
                <el-input
                  v-model="queryParams.name"
                  placeholder="请输入演员姓名"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="性别" prop="sex">
                <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable>
                  <el-option
                    v-for="dict in dict.type.sys_user_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="工作状态" prop="jobstatus">
                <el-select v-model="queryParams.jobstatus" placeholder="请选择工作状态" clearable>
                  <el-option
                    v-for="dict in dict.type.actor_jobstatus"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="合同类型" prop="contracttype">
                <el-select v-model="queryParams.contracttype" placeholder="请选择合同类型" clearable>
                  <el-option
                    v-for="dict in dict.type.sys_cinema_contracttype"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="经纪人姓名" prop="broker">
                <el-input
                  v-model="queryParams.broker"
                  placeholder="请输入经纪人姓名"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
  </el-form>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleAdd"
                  v-hasPermi="['cinema:actor:add']"
                >新增</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="success"
                  plain
                  icon="el-icon-edit"
                  size="mini"
                  :disabled="single"
                  @click="handleUpdate"
                  v-hasPermi="['cinema:actor:edit']"
                >修改</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  size="mini"
                  :disabled="multiple"
                  @click="handleDelete"
                  v-hasPermi="['cinema:actor:remove']"
                >删除</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="warning"
                  plain
                  icon="el-icon-download"
                  size="mini"
                  @click="handleExport"
                  v-hasPermi="['cinema:actor:export']"
                >导出</el-button>
              </el-col>
              <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table v-loading="loading" :data="actorList" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="演员id" align="center" prop="id" />
              <el-table-column label="演员姓名" align="center" prop="name" />
              <el-table-column label="部门" align="center" key="deptName" prop="dept.deptName" :show-overflow-tooltip="true" />
              <el-table-column label="性别" align="center" prop="sex">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
                </template>
              </el-table-column>
              <el-table-column label="头像" align="center" prop="actImage" width="100">
                <template slot-scope="scope">
                  <image-preview :src="scope.row.actImage" :width="50" :height="50"/>
                </template>
              </el-table-column>
              <el-table-column label="身高(CM)" align="center" prop="height" />
              <el-table-column label="体重(KG)" align="center" prop="weight" />
              <el-table-column label="出生日期" align="center" prop="birthday" width="180">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="国籍" align="center" prop="nationality" />
              <el-table-column label="民族" align="center" prop="nation" />
              <el-table-column label="籍贯" align="center" prop="nativeplace" />
              <el-table-column label="演艺经历" align="center" prop="experience" />
              <el-table-column label="技能" align="center" prop="skill" />
              <el-table-column label="代表作品" align="center" prop="masterpiece" />
              <el-table-column label="工作状态" align="center" prop="jobstatus">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.actor_jobstatus" :value="scope.row.jobstatus"/>
                </template>
              </el-table-column>
              <el-table-column label="合同类型" align="center" prop="contracttype">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.sys_cinema_contracttype" :value="scope.row.contracttype"/>
                </template>
              </el-table-column>
              <el-table-column label="合同开始日期" align="center" prop="contractbegindate" width="180">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.contractbegindate, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="合同结束日期" align="center" prop="contractoverdate" width="180">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.contractoverdate, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="是否有经纪人" align="center" prop="ifbroker">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.ifbroker"/>
                </template>
              </el-table-column>
              <el-table-column label="经纪人姓名" align="center" prop="broker" />
              <el-table-column label="经纪人联系方式" align="center" prop="brokerphone" />
              <el-table-column label="其他联系方式" align="center" prop="otherphone" />
              <el-table-column label="备注" align="center" prop="notes" />
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['cinema:actor:edit']"
                  >修改</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['cinema:actor:remove']"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />

    <!-- 添加或修改艺人管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="演员姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入演员姓名" />
        </el-form-item>
        <el-form-item label="归属部门" prop="deptId">
          <treeselect v-model="form.deptId" :options="enabledDeptOptions" :show-count="true" placeholder="请选择归属部门" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="头像" prop="actImage">
          <image-upload v-model="form.actImage"/>
        </el-form-item>
        <el-form-item label="身高(CM)" prop="height">
          <el-input v-model="form.height" placeholder="请输入身高" />
        </el-form-item>
        <el-form-item label="体重(KG)" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入体重" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
          <el-date-picker clearable
                          v-model="form.birthday"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="国籍" prop="nationality">
          <el-input v-model="form.nationality" placeholder="请输入国籍" />
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="籍贯" prop="nativeplace">
          <el-input v-model="form.nativeplace" placeholder="请输入籍贯" />
        </el-form-item>
        <el-form-item label="演艺经历" prop="experience">
          <el-input v-model="form.experience" placeholder="请输入演艺经历" />
        </el-form-item>
        <el-form-item label="技能" prop="skill">
          <el-input v-model="form.skill" placeholder="请输入技能" />
        </el-form-item>
        <el-form-item label="代表作品" prop="masterpiece">
          <el-input v-model="form.masterpiece" placeholder="请输入代表作品" />
        </el-form-item>
        <el-form-item label="工作状态" prop="jobstatus">
          <el-select v-model="form.jobstatus" placeholder="请选择工作状态">
            <el-option
              v-for="dict in dict.type.actor_jobstatus"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同类型" prop="contracttype">
          <el-select v-model="form.contracttype" placeholder="请选择合同类型">
            <el-option
              v-for="dict in dict.type.sys_cinema_contracttype"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同开始日期" prop="contractbegindate">
          <el-date-picker clearable
                          v-model="form.contractbegindate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择合同开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同结束日期" prop="contractoverdate">
          <el-date-picker clearable
                          v-model="form.contractoverdate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择合同结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否有经纪人" prop="ifbroker">
          <el-select v-model="form.ifbroker" placeholder="请选择是否有经纪人">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="经纪人姓名" prop="broker">
          <el-input v-model="form.broker" placeholder="请输入经纪人姓名" />
        </el-form-item>
        <el-form-item label="经纪人联系方式" prop="brokerphone">
          <el-input v-model="form.brokerphone" placeholder="请输入经纪人联系方式" />
        </el-form-item>
        <el-form-item label="其他联系方式" prop="otherphone">
          <el-input v-model="form.otherphone" placeholder="请输入其他联系方式" />
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listActor, getActor, delActor, addActor, updateActor, deptTreeSelect } from "@/api/cinema/actor";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { Splitpanes, Pane } from "splitpanes";
import "splitpanes/dist/splitpanes.css";

export default {
  name: "Actor",
  components: { Treeselect, Splitpanes, Pane },
  dicts: ['actor_jobstatus', 'sys_cinema_contracttype', 'sys_yes_no', 'sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 艺人管理表格数据
      actorList: [],
      // 弹出层标题
      title: "",
      dateRange: [], // 新增日期范围参数
      // 所有部门树选项
      deptOptions: undefined,
      // 过滤掉已禁用部门树选项
      enabledDeptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        deptId: null,
        sex: null,
        actImage: null,
        height: null,
        weight: null,
        birthday: null,
        nationality: null,
        nation: null,
        nativeplace: null,
        experience: null,
        skill: null,
        masterpiece: null,
        jobstatus: null,
        contracttype: null,
        contractbegindate: null,
        contractoverdate: null,
        ifbroker: null,
        broker: null,
        brokerphone: null,
        otherphone: null,
        notes: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: '演员姓名不能为空', trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: '归属部门不能为空', trigger: 'change' }
        ],
        sex: [
          { required: true, message: '性别不能为空', trigger: 'change' }
        ],
        // 添加其他字段的校验规则...
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询艺人管理列表 */
    getList() {
      this.loading = true;
      listActor(this.queryParams).then(response => {
        this.actorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
        this.enabledDeptOptions = this.filterDisabledDept(JSON.parse(JSON.stringify(response.data)));
      });
    },
    // 过滤禁用的部门
    filterDisabledDept(deptList) {
      return deptList.filter(dept => {
        if (dept.disabled) {
          return false;
        }
        if (dept.children && dept.children.length) {
          dept.children = this.filterDisabledDept(dept.children);
        }
        return true;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        deptId: null,
        sex: null,
        actImage: null,
        height: null,
        weight: null,
        birthday: null,
        nationality: null,
        nation: null,
        nativeplace: null,
        experience: null,
        skill: null,
        masterpiece: null,
        jobstatus: null,
        contracttype: null,
        contractbegindate: null,
        contractoverdate: null,
        ifbroker: null,
        broker: null,
        brokerphone: null,
        otherphone: null,
        notes: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.deptId = undefined; // 清空部门ID
      this.$refs.tree.setCurrentKey(null); // 取消树节点选中
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加艺人管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getActor(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改艺人管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateActor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除艺人管理编号为"' + ids + '"的数据项？').then(() => {
        return delActor(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cinema/actor/export', {
        ...this.queryParams
      }, `actor_${new Date().getTime()}.xlsx`);
    }
  }
};
</script>
